<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="银行卡号" prop="bankcardIdcard">
      <el-input v-model="dataForm.bankcardIdcard" placeholder="银行卡号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="bankcardPassword">
      <el-input v-model="dataForm.bankcardPassword" type="password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="bankcardPhone">
      <el-input v-model="dataForm.bankcardPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <!-- <el-form-item label="开户时间" prop="bankcardOpentime">
      <el-input v-model="dataForm.bankcardOpentime" placeholder=""></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="卡余额" prop="bankcardBalance">
      <el-input v-model="dataForm.bankcardBalance" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="银行名称" prop="bankcardBankname">
      <el-input v-model="dataForm.bankcardBankname" placeholder="银行名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bankcardIdcard: '',
          bankcardPassword: '',
          bankcardPhone: '',
          bankcardBankname: ''
        },
        dataRule: {
          bankcardIdcard: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          bankcardPassword: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          bankcardPhone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          bankcardBankname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bankcardId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bankcardId) {
            this.$http({
              url: this.$http.adornUrl(`/efinance/bankcard/info/${this.dataForm.bankcardId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bankcardIdcard = data.bankcard.bankcardIdcard
                this.dataForm.bankcardPassword = data.bankcard.bankcardPassword
                this.dataForm.bankcardPhone = data.bankcard.bankcardPhone
                this.dataForm.bankcardBankname = data.bankcard.bankcardBankname
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/efinance/bankcard/${!this.dataForm.bankcardId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bankcardIdcard': this.dataForm.bankcardIdcard,
                'bankcardPassword': this.dataForm.bankcardPassword,
                'bankcardPhone': this.dataForm.bankcardPhone,
                'bankcardBankname': this.dataForm.bankcardBankname
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
